package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.RadioInterfaceActivity;
import com.hongxun.design.activitys.SetPointInformationActivity;
import com.hongxun.design.bean.SetPointInfItemBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyDecimalFormat;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.sqlite.activity.EditPointLXLocaltionActivity;
import com.hongxun.design.sqlite.activity.EditPointLocaltionActivity;
import com.hongxun.design.ui.ScannerActivity;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.utils.InputUtil;
import com.hongxun.design.utils.PopwindowUtil;
import com.hongxun.design.utils.SPUtils;
import com.pop.android.common.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class SetPointInformationAdapter extends BaseAdapter {
    private List<SetPointInfItemBean> beanList;
    private LayoutInflater inflater;
    private Activity context;

    public SetPointInformationAdapter(List<SetPointInfItemBean> beanList, Activity context) {
        this.beanList = beanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public List<SetPointInfItemBean> getData() {
        return beanList;
    }

    public void setData(int position, String msg, String s, int i) {
        beanList.get(position).setValue(msg);
        beanList.get(position).setValueCode(s);
        beanList.get(position + 1).setValue(i + "");
        beanList.get(position + 1).setValueCode(i + "");
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 8;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Constant.CREATE_GENERAL;
        switch (beanList.get(position).getItemStyle()) {
            case Constant.LatStyle:
                type = Constant.LatStyle;
                break;
            case Constant.EdStyle:
                type = Constant.EdStyle;
                break;
            case Constant.ChoiceStyle:
                type = Constant.ChoiceStyle;
                break;
            case Constant.ButtonStyle:
                type = Constant.ButtonStyle;
                break;
            case Constant.JumpStyle:
                type = Constant.JumpStyle;
                break;
            case Constant.ActivityStyle:
                type = Constant.ActivityStyle;
                break;
            case Constant.dialogstyle:
                type = Constant.dialogstyle;
                break;
            case Constant.ScanStyle:
                type = Constant.ScanStyle;
                break;
        }
        return type;
    }


    private int index = -1;
    public int states = -1;
    public int mposition = 0;
    public int scanPostion = 0;

    public void setScanCode(String code) {
        beanList.get(scanPostion).setValue(code);
        beanList.get(scanPostion).setValueCode(code);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);

        LatHolder latHolder = null;
        EdHolder edHolder = null;
        ChoiceHolder choiceHolder = null;
        ButtonHolder buttonHolder = null;
        ScanHolder scanHolder = null;
        JumpHolder jumpHolder = null;
        ActivityHolder activityHolder = null;
        switch (ViewType) {
            case Constant.LatStyle:
                convertView = inflater.inflate(R.layout.listview_item_setpoint_latstyle, null);
                latHolder = new LatHolder(convertView);
                convertView.setTag(latHolder);
                break;
            case Constant.EdStyle:
                convertView = inflater.inflate(R.layout.listview_item_create_generl, null);
                edHolder = new EdHolder(convertView);
                convertView.setTag(edHolder);
                break;
            case Constant.ChoiceStyle:
                convertView = inflater.inflate(R.layout.listview_item_create_generl, null);
                edHolder = new EdHolder(convertView);
                convertView.setTag(edHolder);
                break;

            case Constant.ButtonStyle:
                convertView = inflater.inflate(R.layout.listview_item_setpoint_buttonstyle, null);
                buttonHolder = new ButtonHolder(convertView);
                convertView.setTag(buttonHolder);
                break;
            case Constant.ScanStyle:
                convertView = inflater.inflate(R.layout.listview_item_setpoint_saomastyle, null);
                scanHolder = new ScanHolder(convertView);
                convertView.setTag(buttonHolder);
                break;
            case Constant.JumpStyle:
                convertView = inflater.inflate(R.layout.listview_item_setpoint_jumpstyle, null);
                jumpHolder = new JumpHolder(convertView);
                convertView.setTag(jumpHolder);
                break;
            case Constant.ActivityStyle:
                convertView = inflater.inflate(R.layout.listview_item_setpoint_activitystyle, null);
                activityHolder = new ActivityHolder(convertView);
                convertView.setTag(activityHolder);
                break;
            case Constant.dialogstyle:
                convertView = inflater.inflate(R.layout.listview_item_create_generl, null);
                edHolder = new EdHolder(convertView);
                convertView.setTag(edHolder);
                break;
        }


        SetPointInfItemBean bean;
        try {
            switch (ViewType) {
                case Constant.EdStyle:
                    bean = beanList.get(position);
                    edHolder.create_name.setText(bean.getName());//项名称
                    setEditText(edHolder, bean);
                    if (bean.getNameCode().equals("laxian")) {
                        edHolder.create_write.setSelectAllOnFocus(true);
                    }
//                    edHolder.create_write.setInputType(bean.getInputType());
                    edHolder.create_write.setOnTouchListener(new View.OnTouchListener() {

                        public boolean onTouch(View view, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                index = position;
                            }
                            return false;
                        }
                    });
                    edHolder.create_write.clearFocus();
                    if (index != -1 && index == position) {
                        // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
                        edHolder.create_write.requestFocus();
                    }
                    break;
                case Constant.LatStyle:
                    bean = beanList.get(position);
                    final double lat = bean.getLat();
                    final double lon = bean.getLon();
                    latHolder.setpoint_itemlat.setText(lat + "");
                    latHolder.setpoint_itemlon.setText(lon + "");
                    latHolder.setpoint_itemaltitude.setText(bean.getAltitude());
                    SpannableString styledText1 = new SpannableString(IconSourse.UPDATE);
                    styledText1.setSpan(new TextAppearanceSpan(context, R.style.style2), 0, styledText1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    SetTypeface.set(latHolder.setpoint_right, styledText1);
//                    latHolder.setpoint_right.append(" 修正");
                    latHolder.setpoint_right.append(" 更新");
                    latHolder.setpoint_right.setOnClickListener(new MyOnClickListener(bean) {
                        @Override
                        public void onClick(SetPointInfItemBean bean, View view) {
                            Bundle bundle = new Bundle();
                            bundle.putDouble("lat", lat);
                            bundle.putDouble("lon", lon);
                            IntentToActivity.intent(context, EditPointLocaltionActivity.class, bundle);
                        }
                    });
                    break;
                case Constant.ChoiceStyle:
                    bean = beanList.get(position);
                    edHolder.create_name.setText(bean.getName());//项名称
                    setPop(edHolder, bean);
                    break;
                case Constant.ScanStyle:
                    bean = beanList.get(position);
                    scanHolder.setpoint_name.setText(bean.getName());
                    scanHolder.setpoint_write.setText(bean.getValue());
                    scanHolder.setpoint_write.addTextChangedListener(bean.getTextWatcher());
                    if (bean.getNameCode().equals(MString.bx_bj_code)) {
                        scanHolder.setpoint_button.setText("设置");
                        scanHolder.setpoint_write.setEnabled(false);
                        scanHolder.setpoint_write.setHint("请设置");
                        scanHolder.setpoint_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for (SetPointInfItemBean s : beanList) {
                                    if (s.getNameCode() != null) {
                                        if (s.getNameCode().equals(MString.bx_new_code) && (s.getValue() == null || s.getValue().equals(""))) {
                                            ToastUtils.showToast(context, "请先输入新表箱条码或者扫码");
                                            return;
                                        }
                                        if (s.getNameCode().equals(MString.bx_old_code)) {
                                            SPUtils.put(context, "old_code", s.getValue() == null ? "" : s.getValue());
                                        } else if (s.getNameCode().equals(MString.bx_new_code)) {
                                            SPUtils.put(context, "new_code", s.getValue());
                                        }
                                    }
                                }
                                ((SetPointInformationActivity) context).tag = 3;
                                ((SetPointInformationActivity) context).choiceHttp();
                            }
                        });
                    } else {
                        scanHolder.setpoint_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(context, ScannerActivity.class);
                                context.startActivityForResult(intent, 9);
                                scanPostion = position;
                            }
                        });
                    }
                    break;
                case Constant.ButtonStyle:
                    bean = beanList.get(position);
                    buttonHolder.setpoint_name.setText(bean.getName());
                    try {
                        if ((bean.getValue().length() != 0 && Double.parseDouble(bean.getValue()) != 0 && MyApp.isOnLine && !SetPointInformationActivity.type.equals("1")) ||
                                (!MyApp.isOnLine && ((SetPointInformationActivity) context).paiziFlag)) {
                            buttonHolder.setpoint_write.setText(bean.getValue());
                            buttonHolder.setpoint_button.setEnabled(true);
                            buttonHolder.setpoint_button.setBackgroundResource(R.drawable.blueline3_wihte_item);
                            buttonHolder.setpoint_button.setTextColor(context.getResources().getColor(R.color.project_blue));
                            buttonHolder.setpoint_button.setOnClickListener(new MClickListener(0, 0) {
                                @Override
                                public void onClick(View view, int position, Object msg) {
                                    LatLng latLng = ((SetPointInformationActivity) context).getLatLng();
                                    if (MyApp.isOnLine) {
                                        if (MyApp.ReadOnly) {
                                            ToastShow.showShort(MyApp.activity, "暂无权限操作");
                                        } else {
                                            MyApp.HTTP.getexp_obj_up(((SetPointInformationActivity) context).cornerHandler, 1, latLng, ((SetPointInformationActivity) context).before_exp_id);
                                        }
                                    } else {
                                        ((SetPointInformationActivity) context).getPaiziClick();
                                    }
                                }
                            });
                        } else {
                            buttonHolder.setpoint_write.setText(bean.getValue());
                            buttonHolder.setpoint_button.setEnabled(false);
                            buttonHolder.setpoint_button.setBackgroundResource(R.drawable.grayline3_wihte_item);
                            buttonHolder.setpoint_button.setTextColor(context.getResources().getColor(R.color.gainsboro));
                        }
                    } catch (Exception e) {
                        buttonHolder.setpoint_write.setText(bean.getValue());
                        buttonHolder.setpoint_button.setEnabled(false);
                        buttonHolder.setpoint_button.setBackgroundResource(R.drawable.grayline3_wihte_item);
                        buttonHolder.setpoint_button.setTextColor(context.getResources().getColor(R.color.gainsboro));
                    }

                    buttonHolder.setpoint_button.setText("排直");

                    break;
                case Constant.JumpStyle:
                    bean = beanList.get(position);
                    mposition = position;
                    jumpHolder.title.setText(bean.getName());
                    if (bean.getValue().length() == 0) {

                        jumpHolder.value.setText(bean.getNovalue());
                        jumpHolder.modify.setTextColor(context.getResources().getColor(R.color.caikan_bule));
                    } else {
                        String s = bean.getValue();
                        if (bean.getValue().length() > 0) {
                            s = MyDecimalFormat.getDouble(Double.parseDouble(bean.getValue()), "0.00");
                        }
                        jumpHolder.value.setText(s);
                        jumpHolder.modify.setTextColor(context.getResources().getColor(R.color.bar_left));
                    }
                    jumpHolder.item.setOnClickListener(bean.getChoiceModelListener());//型号
                    jumpHolder.modify.setOnClickListener(bean.getMaterialListener());//材料明细
                    break;
                case Constant.ActivityStyle:
                    bean = beanList.get(position);
                    activityHolder.title.setText(bean.getName());
                    if (bean.getValue().length() == 0) {
                        activityHolder.value.setText(bean.getNovalue());
                    } else {
                        activityHolder.value.setText(bean.getValue());
                    }
                    activityHolder.item.setOnClickListener(new MClickListener(position, bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            SetPointInfItemBean bean1 = (SetPointInfItemBean) msg;
                            Bundle bundle = new Bundle();
                            bundle.putInt(MString.num, position);
                            bundle.putString(MString.name, bean1.getName());
                            bundle.putString(MString.LIST, bean1.getValueCode());
                            bundle.putString(MString.check, bean1.getValue());
                            bundle.putString(MString.TYPE, MString.setpoint);
                            IntentToActivity.intent(context, RadioInterfaceActivity.class, bundle);
                        }
                    });
                    break;
                case Constant.dialogstyle:
                    bean = beanList.get(position);
                    edHolder.create_name.setText(bean.getName());//项名称
                    edHolder.create_right.setVisibility(View.GONE);
                    edHolder.create_write.setVisibility(View.GONE);
                    edHolder.create_value.setVisibility(View.VISIBLE);

                    if (bean.getValue().length() != 0) {
                        edHolder.create_value.setText(bean.getValue());
                        edHolder.create_value.setTextColor(context.getResources().getColor(R.color.bar_right));
                    } else {
                        edHolder.create_value.setHint(bean.getNovalue());
                        edHolder.create_value.setHintTextColor(context.getResources().getColor(R.color.project_blue));
                    }

                    View.OnClickListener listener = new MClickListener(position, bean, edHolder) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            MyDialog.dialogForNumber(context, msg, msg2);
                        }
                    };
                    edHolder.create_value.setOnClickListener(listener);
                    edHolder.create_item_button.setOnClickListener(listener);
                    break;
            }

        } catch (Exception e) {
            MyLog.L(MyApp.AppName + "适配错误：" + e.toString());
        }

        return convertView;

    }

    abstract class MyOnClickListener implements View.OnClickListener {
        private SetPointInfItemBean bean;

        MyOnClickListener(SetPointInfItemBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            onClick(bean, view);
        }

        public abstract void onClick(SetPointInfItemBean bean, View view);
    }

    //设置edtext类的数据
    private void setEditText(EdHolder generalHolder, SetPointInfItemBean bean) {
        generalHolder.create_write.setVisibility(View.VISIBLE);
        generalHolder.create_value.setVisibility(View.GONE);
        generalHolder.create_right.setVisibility(View.GONE);
        if (bean.getValue() == null && bean.getNameCode().equals("laxian")) {
            generalHolder.create_write.setText("0");
            bean.setValue("0");
            bean.setValueCode("0");
        } else if (bean.getValue() == null || bean.getValueCode() == null) {
            bean.setValue("");
            bean.setValueCode("");
            generalHolder.create_write.setText("");
        } else {
            if (bean.getValue().length() != 0) {
                generalHolder.create_write.setText(bean.getValue());
                generalHolder.create_write.setTextColor(context.getResources().getColor(R.color.bar_right));
            } else {
                //MyLog.L(MyApp.AppName + position + "  setHint=" + bean.getValueUncheck());
                generalHolder.create_write.setHint(bean.getNovalue());
                generalHolder.create_write.setHintTextColor(context.getResources().getColor(R.color.project_blue));
            }
        }
        if (!bean.isEdit()) {
            generalHolder.create_write.setEnabled(false);
        } else {
            generalHolder.create_write.addTextChangedListener(bean.getTextWatcher());
        }
        //拉线定位处理
//        if (bean.getNameCode().equals(MString.laxian)) {
//            generalHolder.create_right.setVisibility(View.VISIBLE);
//            if (bean.getNovalue().equals("1")) {//不可编辑
//                generalHolder.create_write.setEnabled(false);
//            } else {
//                generalHolder.create_write.setEnabled(true);
//            }
//            SetTypeface.set(generalHolder.create_right, IconSourse.RIGHTTOARROW);
//            generalHolder.create_right.setPadding(2, 2, 2, 2);
//            generalHolder.create_right.setOnClickListener(new MyOnClickListener(bean) {
//                @Override
//                public void onClick(SetPointInfItemBean bean, View view) {
//                    IntentToActivity.intent(context, EditPointLXLocaltionActivity.class);
//                }
//            });
//        } else {
//            generalHolder.create_right.setVisibility(View.GONE);
//        }
    }

    private void setPop(final EdHolder edHolder, final SetPointInfItemBean bean) {
        edHolder.create_write.setVisibility(View.GONE);
        edHolder.create_value.setVisibility(View.VISIBLE);
        edHolder.create_right.setVisibility(View.VISIBLE);
//
        if (bean.getValue().length() == 0) {
            edHolder.create_value.setText(bean.getNovalue());
            edHolder.create_value.setHintTextColor(context.getResources().getColor(R.color.project_blue));
        } else {
            edHolder.create_value.setText(bean.getValue());
            edHolder.create_value.setTextColor(context.getResources().getColor(R.color.bar_right));
        }
        edHolder.create_right.setTextColor(context.getResources().getColor(R.color.bar_right));
        SetTypeface.set(edHolder.create_right, IconSourse.DROPDOWN);
        if (bean.isEdit()) {
            edHolder.create_item_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (MyApp.voltage_grade.length() == 0
                            && (bean.getNameCode().equals(MString.wire_model)
                            || bean.getNameCode().equals(MString.wire_id))) {
                        ToastShow.showShort(context, "电压等级不能为空！");

                    } else if (bean.getData() != null && bean.getData().size() > 0) {
                        InputUtil.closeInput(context);
                        WindowManager.LayoutParams params = context.getWindow().getAttributes();
                        params.alpha = 0.7f;
                        context.getWindow().setAttributes(params);
                        ((SetPointInformationActivity) context).map_style.setVisibility(View.VISIBLE);

                        ArrayList<SystemTypeBean> data = bean.getData();
                        ArrayList<SystemTypeBean> data2 = new ArrayList<SystemTypeBean>();

                        if (data != null && data.size() > 0
                                && (bean.getNameCode().equals(MString.wire_model)
                                || bean.getNameCode().equals(MString.wire_id))) {
                            for (SystemTypeBean s : data) {
//                                if (s.code.equals(MyApp.voltage_grade)) {
                                if (s.code.equals("101")) {//仅匹配10kV
                                    data2.add(s);
                                }
                            }
                            PopwindowUtil.popChoice2(context, edHolder.create_value, bean, data2);
                        } else {
                            PopwindowUtil.popChoice2(context, edHolder.create_value, bean, data);
                        }
                    }
                }
            });
        } else {
            edHolder.create_value.setTextColor(context.getResources().getColor(R.color.graybg));
        }
        if (states == 2 && (bean.getNameCode().equals(MString.high_wire_type)
                || bean.getNameCode().equals(MString.high_wire_model) ||
                bean.getNameCode().equals(MString.high_wire_times))) {//高压
            edHolder.create_item_button.setEnabled(false);
            bean.setValue("");
            bean.setValueCode("");
            edHolder.create_value.setText(bean.getNovalue());
            edHolder.create_value.setTextColor(context.getResources().getColor(R.color.graybg));
        } else if (states == 1 && (bean.getNameCode().equals(MString.low_wire_type) ||
                bean.getNameCode().equals(MString.low_wire_model)
                || bean.getNameCode().equals(MString.low_wire_times))) {
            edHolder.create_item_button.setEnabled(false);
            bean.setValue("");
            bean.setValueCode("");
            edHolder.create_value.setText(bean.getNovalue());
            edHolder.create_value.setTextColor(context.getResources().getColor(R.color.graybg));

        } else {
            edHolder.create_item_button.setEnabled(true);
        }

    }

    public class LatHolder {
        public TextView setpoint_type, setpoint_itemlat, setpoint_itemlon, setpoint_itemaltitude, setpoint_right;

        LatHolder(View convertView) {
//            setpoint_type = (TextView) convertView.findViewById(R.id.setpoint_type);
            setpoint_itemlat = (TextView) convertView.findViewById(R.id.setpoint_itemlat);
            setpoint_itemlon = (TextView) convertView.findViewById(R.id.setpoint_itemlon);
            setpoint_itemaltitude = (TextView) convertView.findViewById(R.id.setpoint_itemaltitude);
            setpoint_right = (TextView) convertView.findViewById(R.id.setpoint_right);
        }
    }

    public class EdHolder {

        public TextView create_name, create_value, create_right;
        public EditText create_write;
        public RelativeLayout create_item_button;

        public EdHolder(View convertView) {
            create_item_button = (RelativeLayout) convertView.findViewById(R.id.create_item_button);
            create_name = (TextView) convertView.findViewById(R.id.create_name);
            create_value = (TextView) convertView.findViewById(R.id.create_value);
            create_right = (TextView) convertView.findViewById(R.id.create_right);
            create_write = (EditText) convertView.findViewById(R.id.create_write);

        }
    }

    public class ChoiceHolder {


        ChoiceHolder(View convertView) {

        }
    }

    public class ButtonHolder {
        public TextView setpoint_name, setpoint_write, setpoint_button;

        ButtonHolder(View convertView) {
            setpoint_name = (TextView) convertView.findViewById(R.id.setpoint_name);
            setpoint_write = (TextView) convertView.findViewById(R.id.setpoint_write);
            setpoint_button = (TextView) convertView.findViewById(R.id.setpoint_button);
        }
    }

    public class ScanHolder {
        public TextView setpoint_name, setpoint_button;
        public EditText setpoint_write;

        ScanHolder(View convertView) {
            setpoint_name = (TextView) convertView.findViewById(R.id.setpoint_name);
            setpoint_write = (EditText) convertView.findViewById(R.id.setpoint_write);
            setpoint_button = (TextView) convertView.findViewById(R.id.setpoint_button);
        }
    }

    public class JumpHolder {
        public TextView modify, title, value, right;
        public RelativeLayout item;

        JumpHolder(View convertView) {
            item = (RelativeLayout) convertView.findViewById(R.id.jump_item);
            modify = (TextView) convertView.findViewById(R.id.modify);
            title = (TextView) convertView.findViewById(R.id.jump_title);
            value = (TextView) convertView.findViewById(R.id.jump_value);
            right = (TextView) convertView.findViewById(R.id.jump_right);
            SetTypeface.set(right, IconSourse.RIGHTCHOICE);
            SetTypeface.set(modify, IconSourse.EDIT);
        }
    }

    public class ActivityHolder {
        public TextView title, value, right;
        public RelativeLayout item;

        ActivityHolder(View convertView) {
            item = (RelativeLayout) convertView.findViewById(R.id.jump_item);
            title = (TextView) convertView.findViewById(R.id.jump_title);
            value = (TextView) convertView.findViewById(R.id.jump_value);
            right = (TextView) convertView.findViewById(R.id.jump_right);
            SetTypeface.set(right, IconSourse.RIGHTCHOICE);

        }
    }

}
